﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.BusinessEntities;

namespace NRCP.MetaData
{
    public partial class SubDistrict_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreLoadData();
            }
        }

        private void ClearPage()
        {
            tbName.Text = "";
            ddlDistrict.SelectedIndex = 0;
        }

        private void PreLoadData()
        {
            DistrictBLL bll = new DistrictBLL();
            ddlDistrict.DataSource = bll.GetAllDistricts();
            ddlDistrict.DataTextField = "Name";
            ddlDistrict.DataValueField = "Id";
            ddlDistrict.DataBind();
        }

        protected void bSave_Click(object sender, EventArgs e)
        {
            SubDistrictBLL bll = new SubDistrictBLL();
            SubDistrict subDistrict = new SubDistrict();
            try
            {
                subDistrict.Name = tbName.Text.Trim();
                subDistrict.DistrictId = Convert.ToInt32(ddlDistrict.SelectedItem.Value);
                bll.CreateNewSubDistrict(subDistrict);
                rm.SetSuccessMessage("Add Sub District Successful.");
                ClearPage();
            }
            catch (Exception ex)
            {
                rm.SetFailMessage("Failed To Add Sub District. " + ex.Message);
            }
        }
    }
}